<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备名字" prop="deviceName">
      <el-input v-model="dataForm.deviceName" placeholder="设备名字"></el-input>
    </el-form-item>
    <!--<el-form-item label="产品key" prop="productKey">-->
      <!--<el-input v-model="dataForm.productKey" placeholder="产品key"></el-input>-->
    <!--</el-form-item>-->
      <el-form-item label="选择产品" prop="agentGrade">
        <el-select v-model="productId"  placeholder="选择产品" style="width: 300px" >
          <el-option
            v-for="item in productList"
            :key="item.id"
            :label="item.name"
            :value="item.id" >
          </el-option>
        </el-select>
      </el-form-item>



    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        productList: [],
        productId: 0,
        dataForm: {
          id: 0,
          deviceName: '',
          productKey: ''
        },
        dataRule: {
          deviceName: [
            { required: true, message: '设备名字不能为空', trigger: 'blur' }
          ],
          productKey: [
            { required: true, message: '产品密钥不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created () {
      this.getProductList()
    },
    methods: {
      getProductList () {
        this.$http({
          url: this.$http.adornUrl('/generator/tproduct/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': 1,
            'limit': 50
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.productList = data.page.list
          } else {
            this.productList = []
          }
        })
      },
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          // if (this.dataForm.id) {
          //   this.$http({
          //     url: this.$http.adornUrl(`/generator/tdevice/info/${this.dataForm.id}`),
          //     method: 'get',
          //     params: this.$http.adornParams()
          //   }).then(({data}) => {
          //     if (data && data.code === 0) {
          //       this.dataForm.deviceName = data.tdevice.deviceName
          //       this.dataForm.deviceSecret = data.tdevice.deviceSecret
          //       this.dataForm.productId = data.tdevice.productId
          //       this.dataForm.status = data.tdevice.status
          //       this.dataForm.flag = data.tdevice.flag
          //       this.dataForm.activeDate = data.tdevice.activeDate
          //       this.dataForm.createDate = data.tdevice.createDate
          //       this.dataForm.modifyDate = data.tdevice.modifyDate
          //     }
          //   })
          // }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/tdevice/registerDevice`),
              method: 'post',
              data: this.$http.adornData({
                // 'id': this.dataForm.id || undefined,
                'deviceName': this.dataForm.deviceName,
                'productId': this.productId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
